package com.group02.community.service.impl;


import com.alibaba.excel.EasyExcel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.log.SysLogininfor;
import com.group02.community.mapper.SysLogininforMapper;
import com.group02.community.service.SysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 * 系统访问记录 服务实现类
 * </p>
 *
 * @author xl
 * @since 2022-07-01
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor> implements SysLogininforService {

    @Autowired
    private SysLogininforMapper sysLogininforMapper;

    @Override
    public IPage<SysLogininfor> loginLogPage(Long current, Long size, SysLogininfor sysLogininfor, String dateRange) {
        if (dateRange!=null && dateRange!=""){
            String[] split = dateRange.split(",");
            sysLogininfor.setStartTime(split[0]);
            sysLogininfor.setEndTime(split[1]);
        }
        IPage<SysLogininfor> sysLogininforIPage =sysLogininforMapper.loginLogPage(new Page<SysLogininfor>(current,size),sysLogininfor);
        return sysLogininforIPage;
    }

    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        sysLogininforMapper.insertLogininfor(logininfor);
    }

    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        //查询所有数据
        List<SysLogininfor> sysDictTypes = sysLogininforMapper.selectList(null);
        //设置文件下载
        //设置响应头，告诉浏览器要以文件的形式保存
//        response.setHeader();
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlms");
        //生成Excel并写入
        EasyExcel.write(response.getOutputStream(), SysLogininfor.class).sheet(System.currentTimeMillis()+"").doWrite(sysDictTypes);
    }

    @Override
    public void emptyDatabase() {
        sysLogininforMapper.emptyDatabase();
    }

    @Override
    public void loginLogDelete(Long[] id) {
        sysLogininforMapper.loginLogDelete(id);
    }
}
